﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using PopupControl;
using SharpDiary.Core.ORMapping;

namespace SharpDiary.Core.Controls
{
    public partial class DiaryTitleAndTagsControl : UserControl
    {
        #region Fields & Properties

        private TagSelector _tagSelector = new TagSelector();
        private EmotionEditor _emotionEditor = new EmotionEditor();

        private bool _isModified = false;

        public bool IsModified
        {
            get { return _isModified; }
            set
            {
                _isModified = value;
                OnModifiedChanged(this, EventArgs.Empty);
            }
        }

        public string Title
        {
            get { return txtTitle.Text; }
            set
            {
                txtTitle.Text = value;

                IsModified = true;
            }
        }

        private bool _borderVisible = true;

        public bool BordersVisible
        {
            get { return _borderVisible; }
            set
            {
                _borderVisible = value;
                pnlContainer.UseBorderStyleProperty = !_borderVisible;
            }
        }

        public string Tags
        {
            get { return _tagSelector.Tags; }
            set
            {
                _tagSelector.Tags = value;
                IsModified = true;
            }
        }

        public EmotionInfo Emotion
        {
            get { return _emotionEditor.Emotion; }
            set
            {
                _emotionEditor.Emotion = value;
                IsModified = true;
            }
        }

        #endregion

        #region Constructors

        public DiaryTitleAndTagsControl()
        {
            InitializeComponent();

            ResizeRedraw = true;
            DoubleBuffered = true;

            txtTitle.ModifiedChanged += new EventHandler(txtTitle_ModifiedChanged);
            _tagSelector.ModifiedChanged += new EventHandler<EventArgs>(_tagSelector_ModifiedChanged);
            _emotionEditor.ModifiedChanged += new EventHandler<EventArgs>(_emotionEditor_ModifiedChanged);
        }

        void _emotionEditor_ModifiedChanged(object sender, EventArgs e)
        {
            IsModified = true;
        }

        void _tagSelector_ModifiedChanged(object sender, EventArgs e)
        {
            IsModified = true;
        }

        void txtTitle_ModifiedChanged(object sender, EventArgs e)
        {
            IsModified = true;
        }

        #endregion

        /// <summary>
        /// 内容改变后触发此事件。
        /// </summary>
        public event EventHandler<EventArgs> ModifiedChanged;

        protected virtual void OnModifiedChanged(object sender, EventArgs e)
        {
            if (ModifiedChanged != null)
            {
                ModifiedChanged(sender, e);
            }
        }

        private void btnTags_Click(object sender, EventArgs e)
        {
            Popup popup = new Popup(_tagSelector);
            popup.Width = this.Width;
            popup.Show(this);
        }

        private void btnFeeling_Click(object sender, EventArgs e)
        {
            _emotionEditor.ShowEmotions();
            Popup popup = new Popup(_emotionEditor);
            popup.Width = this.Width;
            popup.Show(this);
        }
    }
}
